package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type Order struct {
	ID                 string  `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键,订单号"`
	BatchID            string  `json:"batch_id" gorm:"column:batch_id;type:varchar(50);comment:批次号"`
	Openid             string  `json:"openid" gorm:"index:idx_openid_goodsID,priority:1,type:btree;column:openid;type:varchar(50);comment:openid"`
	RobotID            string  `json:"robot_id" gorm:"column:robot_id;type:varchar(50);comment:机器名称"`
	OrderStatus        int     `json:"order_status" gorm:"column:order_status;type:int;size:64;comment:订单状态"`
	GoodsID            string  `json:"goods_id" gorm:"index:idx_openid_goodsID,priority:2,type:btree;column:goods_id;type:varchar(30);comment:商品id"`
	GoodsType          int     `json:"goods_type" gorm:"column:goods_type;type:int;size:64;comment:商品类型"`
	GoodsName          string  `json:"goods_name" gorm:"column:goods_name;type:varchar(200);comment:商品名称"`
	Count              int     `json:"count" gorm:"column:count;type:int;size:64;comment:数量"`
	Univalent          float64 `json:"univalent" gorm:"column:univalent;type:decimal(10,2);default:0.00;comment:商品金额"`
	Amount             float64 `json:"amount" gorm:"column:amount;type:double;comment:商品总价格"`
	Points             float64 `json:"points" gorm:"column:points;type:double;comment:积分"`
	ExpressFee         float64 `json:"express_fee" gorm:"column:express_fee;type:double;comment:快递费"`
	PurchaseMethod     int     `json:"purchase_method" gorm:"column:purchase_method;type:int;size:64;default:1;comment:购买方式:1站点自提,2快递到家"`
	Address            string  `json:"address" gorm:"column:address;type:varchar(50);comment:收货地址"`
	PickupCode         string  `json:"pickup_code" gorm:"column:pickup_code;type:varchar(20);comment:取货码"`
	PickupStatus       int64   `json:"pickup_status" gorm:"column:pickup_status;type:int;size:64;comment:取货状态:0未取货,1已取货,2已超时"`
	PickupDeadline     int64   `json:"pickup_deadline" gorm:"column:pickup_deadline;type:int;size:64;comment:取货截止时间"`
	CompletedAt        int64   `json:"completed_at" gorm:"column:completed_at;type:bigint;comment:订单完成时间"`
	CreatedAt          int64   `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	UpdatedAt          int64   `json:"updated_at" gorm:"column:updated_at;type:int;size:64;comment:更新时间"`
	RefundAt           int64   `json:"refund_at" gorm:"column:refund_at;type:int;size:64;comment:退款时间"`
	OnlineEventID      string  `json:"online_event_id" gorm:"column:online_event_id;type:int;size:64;default:null;comment:活动id"`
	OnlineEventEndTime int64   `json:"online_event_end_time,omitempty" gorm:"-"`
	ReviewID           string  `json:"review_id" gorm:"column:review_id;type:int;size:64;default:null;comment:评测id"`
	// 收货信息
	UserName      string `json:"user_name,omitempty" gorm:"column:user_name;type:string;size:30;comment:收件人姓名"`
	TelNumber     string `json:"tel_number,omitempty" gorm:"column:tel_number;type:string;size:20;comment:收货人手机号"`
	PostalCode    string `json:"postal_code,omitempty" gorm:"column:postal_code;type:string;size:255;comment:邮编"`
	ProvinceName  string `json:"province_name,omitempty" gorm:"column:province_name;type:string;size:10;comment:国标收货地址第一级地址"`
	CityName      string `json:"city_name,omitempty" gorm:"column:city_name;type:string;size:10;comment:国标收货地址第二级地址"`
	CountyName    string `json:"county_name,omitempty" gorm:"column:county_name;type:string;size:20;comment:国标收货地址第三级地址"`
	StreetName    string `json:"street_name,omitempty" gorm:"column:street_name;type:string;size:20;comment:国标收货地址第四级地址"`
	DetailAddress string `json:"detail_address,omitempty" gorm:"column:detail_address;type:string;size:50;comment:详细地址"`
	// 非数据库字段
	GoodsImage     string  `json:"goods_image" gorm:"-"`
	RobotLocation  string  `json:"robot_location" gorm:"-"`
	RobotFloor     string  `json:"robot_floor" gorm:"-"`
	RobotAddr      string  `json:"robot_addr" gorm:"-"`
	RobotLongitude float32 `json:"robot_longitude" gorm:"-"`
	RobotLatitude  float32 `json:"robot_latitude" gorm:"-"`
	DeductionLimit float64 `json:"deduction_limit" gorm:"-"`
}

func (Order) TableName() string {
	return "order"
}

// BeforeCreate 自增id
func (m *Order) BeforeCreate(tx *gorm.DB) (err error) {
	if m.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	m.ID = uuid
	if m.BatchID == "" {
		m.BatchID = m.ID
	}
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &Order{})
}
